package com.shundr.auth.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import com.shundr.auth.frame.util.LLogger;

/**
 * 文件操作帮助类
 * 
 * @author zhaoyy 2015-06-04
 *
 */
public class FileHelper {
	/**
	 * 复制文件
	 * 
	 * @param src
	 *            源文件
	 * @param dest
	 *            目的文件
	 * @throws IOException
	 */
	public static void copyFile(File src, File dest) throws IOException {
		if (!src.exists()) {
			throw new FileNotFoundException("source file not found.");
		}
		// make parent directory if not exists
		dest.getParentFile().mkdirs();
		FileInputStream inStream = null;
		FileOutputStream outStream = null;
		FileChannel in = null;
		FileChannel out = null;

		try {
			inStream = new FileInputStream(src);
			outStream = new FileOutputStream(dest);
			in = inStream.getChannel();
			out = outStream.getChannel();
			in.transferTo(0, in.size(), out);
		} catch (IOException e) {
			throw e;
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
			if (inStream != null) {
				inStream.close();
			}
			if (outStream != null) {
				outStream.close();
			}
		}

	}
}
